<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\captcha\Captcha;
use app\admin\model\ConfigModel;


class Home extends Controller
{
    public function __construct ( Request $request = null )
    {
        parent::__construct( $request );
        //将系统配置的值传入常量
        if(!empty(session("home"))){
            $url = $this->getPowerUrl(session("home.id"));
        }


    }

    public function getPowerUrl($id)
    {
        $user = model("user");
        $list = $user->getUserPowerUrl($id);
        return $list;
    }


    public function judgeAction ($id)
    {
        $urlList = $this->getPowerUrl($id);
        if(!empty($urlList)) {
            foreach ($urlList as $key => $value) {
                $url[$key] = $value['url'];
            }
//            dump($_SERVER['REQUEST_URI']);
//            dump(Request::instance()->controller());
//            dump(Request::instance()->module());
//            dump(Request::instance()->action());
            $now_url = "/" . Request::instance()->module() . "/" . Request::instance()->controller() . "/" . Request::instance()->action();
            $action = in_array(strtolower($now_url), $url);
//            dump($url);
//            dump($action);
//            die;
            if(!$action){
                $this->error("您没有权限执行此操作");
            }
        }else{
            $this->error("您没有权限执行此操作");
        }
    }

    public function web_config()
    {
        $a = (new ConfigModel());
        // define('WEB_NAME' ,'1');
        $conf = array(
            'web_name' => $web_name = $a->web_name(), 
            'record_number' =>$record_number = $a->record_number() , 
            'zz_email' =>$zz_email = $a->zz_email(), 
            'web_keywords' =>$web_keywords = $a->web_keywords() , 
            'web_description' => $web_description = $a->web_description(), 
            'qq_id' =>$qq_id = $a->qq_id(), 
            'qq_key' =>  $qq_key = $a->qq_key(), 
            'baidu_site' => $baidu_site = $a->baidu_site() , 
            'statistics' => $statistics = $a->statistics(), 
            'email_smtp' =>$email_smtp = $a->email_smtp() , 
            'email_name' =>$email_name = $a->email_name() , 
            'email_pwd' =>$email_pwd = $a->email_pwd() , 
            'email_nickname' =>  $email_nickname = $a->email_nickname()
        );
        //将系统参数设为常量
        define('WEB_NAME' , $conf['web_name']['config_val']); 
        define('RECORD_NUMBER' , $conf['record_number']['config_val']); 
        define('ZZ_EMAIL' , $conf['zz_email']['config_val']); 
        define('WEB_KEYWORDS' , $conf['web_keywords']['config_val']); 
        define('WEB_DESCRIPTION' , $conf['web_description']['config_val']); 
        define('QQ_ID' , $conf['qq_id']['config_val']); 
        define('QQ_KEY' , $conf['qq_key']['config_val']); 
        define('BAIDU_SITE' , $conf['baidu_site']['config_val']); 
        define('STATISTICS' , $conf['statistics']['config_val']); 
        define('EMAIL_SMTP' , $conf['email_smtp']['config_val']); 
        define('EMAIL_NAME' , $conf['email_name']['config_val']); 
        define('EMAIL_PWD' , $conf['email_pwd']['config_val']); 
        define('EMAIL_NICKNAME' , $conf['email_nickname']['config_val']); 

    }



    /**
     * 方法名不正确时,自动调到404页面
     * @param $var 错误的方法名
     * @return \think\response\View
     */
    public function _empty($var)
    {
        $this->assign("title","404");
        return view("Error/404"); 
    }

    /**
     * 判断用户是否登录
     */
    public function isLogin ()
    {
    	if(empty(session("home"))){
    		$this->success("请先登录",url("User/login"));
    	}
    	
    }

    /**
     * 判断验证码是否正确
     * @param $code
     * @return bool
     */
    public function checkVerify ($code)
    {
        $captcha = new Captcha();
        return $captcha->check($code);
    }
    
}
?>